/*
 * @Author: 
 * @Date: 2024-06-11 16:08:43
 * @LastEditor: zhuxiaoyi
 * @LastEditTime: 2024-06-11 16:55:37
 * @Description: 
 */
import { Provide, Config } from '@midwayjs/core';
import { AlgrithmType } from '../entity/algorithmType.entity';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, Like } from 'typeorm';

@Provide()
export class AlgorithmTypeService {
    @InjectEntityModel(AlgrithmType)
    AlgrithmTypeModel: Repository<AlgrithmType>;

    async algorithmTypes() {
        const algorithmTypes = await this.AlgrithmTypeModel.find({
            order: { createDate: 'ASC' },
        });
        const treeData = this.buildTree(algorithmTypes);
        return {
            succes: true,
            data: treeData,
            message: '成功',
        };
    }

    buildTree(data, parentId = '0') {
        const tree = [];
        data.forEach(item => {
            if (item.parentId === parentId) {
                const children = this.buildTree(data, item.id.toString());
                if (children.length) {
                    item.children = children;
                }
                tree.push(item);
            }
        });
        return tree;
    }

}